package com.lenovo.manufacture;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ProductWarehouseActivity extends AppCompatActivity {

    private ListView listView;
    private final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private ProductData data;
    private CarTypeData carTypeData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_warehouse);
        initView();
        getProduct();


    }

    private void getProduct() {
        Network network = retrofit.create(Network.class);
        Call<ProductData> call = network.getProductdata();
        call.enqueue(new Callback<ProductData>() {
            @Override
            public void onResponse(Call<ProductData> call, Response<ProductData> response) {
                data = response.body();

                Network network = retrofit.create(Network.class);
                Call<CarTypeData> carType = network.getCarType();
                carType.enqueue(new Callback<CarTypeData>() {
                    @Override
                    public void onResponse(Call<CarTypeData> call, Response<CarTypeData> response) {
                        carTypeData=response.body();
                        MyAdapter myAdapter = new MyAdapter();
                        listView.setAdapter(myAdapter);
                    }

                    @Override
                    public void onFailure(Call<CarTypeData> call, Throwable t) {

                    }
                });


            }

            @Override
            public void onFailure(Call<ProductData> call, Throwable t) {

            }
        });

    }

    public class MyAdapter extends BaseAdapter {

        private TextView idText;
        private TextView cartypeText;
        private TextView numText;
        private TextView landText;
        private TextView productionText;
        private TextView stateText;

        @Override
        public int getCount() {
            return data.getData().size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(ProductWarehouseActivity.this).inflate(R.layout.row, viewGroup, false);
            idText = view.findViewById(R.id.id_text);
            cartypeText = view.findViewById(R.id.cartype_text);
            numText = view.findViewById(R.id.num_text);
            landText = view.findViewById(R.id.land_text);
            productionText = view.findViewById(R.id.production_text);
            stateText = view.findViewById(R.id.state_text);
            idText.setText(String.valueOf(data.getData().get(i).getId()));
            if (data.getData().get(i).getCarTypeId()==1){
                cartypeText.setText(carTypeData.getData().get(1).getCarTypeName());
                landText.setText(String.valueOf(carTypeData.getData().get(1).getSize()));
                productionText.setText("轿车车型生产线");
            }else if (data.getData().get(i).getCarTypeId()==2){
                cartypeText.setText(carTypeData.getData().get(0).getCarTypeName());
                landText.setText(String.valueOf(carTypeData.getData().get(0).getSize()));
                productionText.setText("MVP车型生产线");
            }else {

                cartypeText.setText(carTypeData.getData().get(2).getCarTypeName());
                landText.setText(String.valueOf(carTypeData.getData().get(2).getSize()));
                productionText.setText("SUV车型生产线");
            }
            numText.setText(String.valueOf(data.getData().get(i).getNum()));
            stateText.setText("维修");

//            idText.setText(String.valueOf(data.getData().get(0).g));
//            cartypeText.setText();
//            stateText.setText();

            return view;
        }

    }

    private void initView() {
        listView = (ListView) findViewById(R.id.list_view);

    }
}